#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time   : 2021/3/25 20:17
# @Author : cjw
import requests
from lxml import etree
from time import sleep


def get_video(url):
    """
    爬取梨视频:
    1、拿到contId
    2、拿到videoStatus返回的json -> srcURL
    3、对srcURL里面的内容进行修改
    4、下载视频
    :return:
    """
    # url = 'https://www.pearvideo.com/video_1724391'
    cont_id = url.split('_')[1]
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36',
        'Referer': url  # 防盗链：需要增加请求源
    }
    video_status_url = f'https://www.pearvideo.com/videoStatus.jsp?contId={cont_id}&mrd=0.3308871515443732'
    # 获取真实视频地址
    resp = requests.get(video_status_url, headers=headers)
    result_dict = resp.json()
    src_url = result_dict['videoInfo']['videos']['srcUrl']
    system_time = result_dict['systemTime']
    src_url = src_url.replace(system_time, f'cont-{cont_id}')
    resp.close()

    # 视频下载
    with open(f'./mp4/{cont_id}.mp4', 'wb') as f:
        resp = requests.get(src_url)
        f.write(resp.content)
        resp.close()
    sleep(0.5)
    print(f'下载完成：{url}')


def get_popular_url():
    """
    获取排行榜的文字url
    :return:
    """
    url = 'https://www.pearvideo.com/popular'
    index_url = 'https://www.pearvideo.com/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'
    }
    resp = requests.get(url, headers=headers)
    page_text = resp.text
    html = etree.HTML(page_text)
    li_list = html.xpath('//ul[@id="popularList"]/li')
    url_list = [index_url + li.xpath('./a/@href')[0] for li in li_list]
    resp.close()
    return url_list


if __name__ == '__main__':
    # get_video()
    # get_popular_url()
    for url in get_popular_url():
        get_video(url)
